<?php
namespace Swiftx\Bundle\WeChat\Controller\Company;
use Swiftx\Bundle\WeChat\Factory\Company as CompanyFactory;
use Swiftx\Bundle\WeChat\Interfaces\Common\BaseController;
use Swiftx\Bundle\WeChat\Interfaces\Controller\Company\Login as LoginInterface;
use Swiftx\Bundle\WeChat\Interfaces\View\Login as LoginView;
use Swiftx\Bundle\WeChat\Support\Verify;
use Swiftx\Interfaces\Httpd\Request;
use Swiftx\Interfaces\Httpd\Response;
use Swiftx\Interfaces\Orm\Manager;

/**
 * 用户登录页面实现
 */
class Login extends BaseController implements LoginInterface {

    /**
     * 实体管理器
     * @var Manager
     * @auto-injection
     */
    protected $entityManager;

    /**
     * 企业工厂实例
     * @var CompanyFactory
     * @auto-injection
     */
    protected $companyFactory;

    /**
     * 显示图片验证码
     * @param Request $request
     * @param Response $response
     * @return bool
     */
    public function showVerifyImage(Request $request, Response $response): bool{
        $verify = new Verify();
        $request->getSession()->set('captcha',$verify->getCode());
        $verify->ShowImage();
        return true;
    }

    /**
     * 验证用户身份操作
     * @param Request $request
     * @param Response $response
     * @router method   post
     * @return bool
     */
    public function actionVerifyIdentity(Request $request, Response $response): bool{
        // 检查请求参数有效性
        $post = $request->getPost();
        // 校验验证码有效性
        if(!$post->exists('verify'))
            return $this->displayError('验证码不能为空!', $response);
        $session = $request->getSession();
        if(!$session->exists('captcha'))
            return $this->displayError('验证码无效!', $response);
        $captcha = strtolower($session->get('captcha'));
        $session->clean('captcha');
        if(strtolower($post->get('verify')) != $captcha)
            return $this->displayError('验证码不正确!', $response);
        // 校验账号有效性
        if(!$post->exists('email'))
            return $this->displayError('账号不能为空!', $response);
        // 校验密码有效性
        if(!$post->exists('password'))
            return $this->displayError('密码不能为空!', $response);
        // 接收参数并验证格式
        $email = $post->get('email');
        $password = $post->get('password');
        if(!$this->isEmail($email))
            return $this->displayError('邮箱不正确!', $response);
        if(!$this->isPassword($password))
            return $this->displayError('密码格式不正确!', $response);
        // 根据用户名查找用户
        $company = $this->companyFactory->fetchByEmail($email);
        if($company == null)
            return $this->displayError('邮箱未注册!', $response);
        if(!$company->checkPassword($password))
            return $this->displayError('登陆密码不正确!', $response);
        $this->companyFactory->registerLogin($request, $company);
        return $this->displaySuccess($response);
    }

    /**
     * 显示出错结果
     * @param string $message
     * @param Response $response
     * @return bool
     */
    public function displayError(string $message, Response $response):bool{
        $message = $this->createView()->errorMessage($message);
        $response->echo($message);
        return true;
    }

    /**
     * 显示成功结果
     * @param Response $response
     * @return bool
     */
    public function displaySuccess(Response $response):bool{
        $message = $this->createView()->successMessage();
        $response->echo($message);
        return true;
    }

    /**
     * 创建视图对象
     * @return LoginView
     */
    protected function createView():LoginView{
        return $this->container->fetch(LoginView::class);
    }

    /**
     * 验证是否是邮箱
     * @param string $value
     * @return bool
     */
    protected function isEmail(string $value):bool{
        return preg_match('/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/', $value);
    }

    /**
     * 验证是否是用户名
     * @param string $value
     * @return bool
     */
    protected function isUsername(string $value):bool{
        return preg_match('/^[a-zA-Z0-9_]{3,16}$/', $value);
    }

    /**
     * 验证是否是用户名
     * @param string $value
     * @return bool
     */
    protected function isPassword(string $value):bool{
        return preg_match('/^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$/', $value);
    }

}